import Vue from "vue";
import Vuex from 'vuex';

// import pl from "element-ui/src/locale/lang/pl";
//使用Vuex
Vue.use(Vuex)

//创建Vuex实例
const store =new Vuex.Store({
    state: {
        token: '',
        username:'',
        authority:{},
        menuList: [],
    },

    getters:{
    },
    mutations: {
        setToken: (state,token)=>{
            state.token = token
            sessionStorage.setItem("token",token)
            const { 1: encodedPayload} = token.split('.');
            let Base64 = require('js-base64').Base64;
            let playload= Base64.decode(encodedPayload)
            let json=JSON.parse(playload)
            let {sub,authority}=json
            state.username=sub;
            let obj={}
            for (const objKey of authority.split(',')) {
                obj[objKey]=true
            }
            state.authority=obj
        },
        resetState(){
            this.state.token = '';
        },
        initRoutes(state,data){
            this.state.menuList = data;
        },
    },
    actions: {

    },

})

export default store //导出store 文件末尾要换行
